<?php
/*
Plugin Name: Manage Your Own Items
Version: 0.2
Plugin URI: http://webheadcoder.com/manage-your-own-items
Description: When you have multiple blog authors that can't edit other's posts and you wouldn't want them to see other posts, use this plugin to hide posts from other authors.  This plugin will only show the author's own posts and not any other author's posts (does not apply to admin).  Similarly, it will only show the author's uploads in the Media Library.
Author: Webhead LLC
Author URI: http://webheadcoder.com
*/

if (!defined('WH_MYOI_VERSION')) {
    add_action('init', 'wh_myoi_embedded_init');
}

function wh_myoi_embedded_init() {
    if (!defined('WH_MYOI_VERSION')) {
        define('WH_MYOI_VERSION', '0.1');
    }
    if (is_admin()) {
        add_action('load-edit.php', 'wh_myoi_admin_post_load_hook');
        add_action('load-upload.php', 'wh_myoi_admin_media_load_hook');
        add_action('load-media-upload.php', 'wh_myoi_admin_media_load_hook');
    }
}

function wh_myoi_admin_post_load_hook() {
    wh_myoi_loadStatuses();
    add_filter('ngettext_with_context', 'wh_myoi_change_mine_label', 10, 5);
    add_filter('ngettext', 'wh_myoi_change_mine_label', 10, 5);
}


function wh_myoi_admin_media_load_hook() {
    add_filter('parse_query', 'wh_myoi_parse_query_useronly' );
}

/**
 * Only load the filtering of the author after wp has loaded fully and all the post-types are registered.
 */
function wh_myoi_loadStatuses() {
    global $wh_myoi_statuses;
    add_filter('pre_get_posts', 'wh_myoi_parse_query_useronly' );
    $post_types = get_post_types();
    foreach ($post_types as $post_type_name=>$post_type_value) {
        add_filter('views_edit-' . $post_type_name, 'wh_myoi_filter_views_edit_posts');
    }
    $wh_myoi_statuses = get_post_stati(array('show_in_admin_status_list' => true));
}


/**
 * Hide the "All" label.  The counts are still as if all the posts were retrieved.  
 * I don't see any hook or filter to alter the count, so just hide the label.
 */
function wh_myoi_filter_views_edit_posts( $views ) {
    if ( !current_user_can( 'edit_others_posts' ) ) :
        unset($views['all']);
    endif;
    return $views;
}

/**
 * Change the "Mine" label to read as "All"
 */
function wh_myoi_change_mine_label($translation, $single, $plural, $number, $context) {
    global $post_type, $wh_myoi_statuses, $wpdb;
    
    if ( current_user_can( 'edit_others_posts' ) ) return $translation;

    if ($context == 'posts') :
        if ($translation == 'Mine <span class="count">(%s)</span>') :
            return 'All <span class="count">(%s)</span>';
        endif;
    else :
        foreach ($wh_myoi_statuses as $status=>$status_text) :
            if (stripos($translation, $status) === FALSE) continue;
            //string matched.  use this query from class-wp-posts-list-table.php
            $status_count = $wpdb->get_var( $wpdb->prepare( "
              SELECT COUNT( 1 ) FROM $wpdb->posts
              WHERE post_type = %s AND post_status = '%s'
              AND post_author = %d
            ", $post_type, $status, get_current_user_id() ) );
              return sprintf($translation, $status_count);
        endforeach;
    endif;
    return $translation;
}


//Manage Your Media Only
function wh_myoi_parse_query_useronly( $wp_query ) {
    if ( !current_user_can( 'edit_others_posts' ) ) {
        global $current_user;
        $wp_query->set( 'author', $current_user->id );
    }
}


?>